function gameReady() {
	var that;
}

gameReady.prototype = {
	time:30,	//游戏剩余时间
	aud:null,
	audA:null,
	maxDifference:145,
	bgMusic:'',
	init_url:"/tug/screen/wait",
	check_url:"/tug/screen/headcount",
	get_power_url:'/tug/screen/tugging',
	get_participation_url:'/tug/screen/start',
	game_over_url:'/tug/screen/over',
	game_time:'/tug/screen/getTime',
	statistic_url:'/tug/screen/statistic',
	meet_id:'',
	sequence:'',
	game_params: {},
	checkTimer:null,
	getTimer :'',
	effectHorseTimer:null,
	powerobj_l:null,
	powerobj_r:null,
	isSuportCssFlag:'',
	duration:5,			// readygo音乐时间
	positionLeft: ($("#arena-man").width()) / 2,		// 擂台页擂台人员gif宽度
	transition:["transition","msTransition","webkitTransition","mozTransition"],
	init:function() {
		that = this;
		that.property = that.isSuportCss(that.transition);
		that.aud = document.getElementById("aud");
		that.audA = document.getElementById("audA");
		that.meet_id = that.getQueryString("meet_id");
		that.sequence = that.getQueryString("sequence");
		that.powerobj_l = $("#power-num-l");
		that.powerobj_r = $("#power-num-r");
		that._effectHorse();
		that._getGameTeam();
		that._checkJoinPeople();
	},
	// 大屏初始化
	_getGameTeam:function() {
		var data = {meet_id:that.meet_id,sequence:that.sequence};
		that.request_data(that.init_url,data,that._getGameTeamCallBack,that._getGameTeamErrorCallBack);
	},
	_getGameTeamCallBack:function(backData) {
		if(parseInt(backData.reCode) == 1) {
			var gameInfo = backData.gameInfo;
			var redInfo = backData.redInfo;
			var blueInfo = backData.blueInfo;
			var roundNum = gameInfo['round'];
			that.bgMusic = gameInfo.bgMusic;
			that.playMusic(that.audA,'start',that.bgMusic);
			var red_name = redInfo.team_name;
			var red_image = redInfo.team_image;
			var blue_name = blueInfo.team_name;
			var blue_image =blueInfo.team_image;

			//首屏数据填充 //左边的红队 右边是蓝队
			var leftObj = $("#section1").children(".left");
			var rightObj = $("#section1").children(".right");
			leftObj.find(".team-img").attr("src",red_image);
			leftObj.find(".team-name").html(red_name);
			rightObj.find(".team-img").attr("src",blue_image);
			rightObj.find(".team-name").html(blue_name);
			if( roundNum < 10){
				$(".round").after('<span class="numb numb'+roundNum+'"></span>');
			}else if(9 > roundNum < 100) {
				var first = parseInt(roundNum / 10);
				var second = roundNum % 10;
				$(".round").after('<span class="numb numb' + first + '"></span><span class="numb numb' + second + '"></span>');
			}

			//擂台页数据填充
			var leftObj_2 = $("#section2").children(".nav").children(".nav-l");
			var rightObj_2 = $("#section2").children(".nav").children(".nav-r");
				leftObj_2.find(".team-name").children("p").html(red_name);
				leftObj_2.find(".team-img").attr("src",red_image);

				rightObj_2.find(".team-name").children("p").html(blue_name);
				rightObj_2.find(".team-img").attr("src",blue_image);

			that.game_params.round = gameInfo.round;
			that.game_params.time = gameInfo.gameTime;
			that.game_params.limit = parseInt(gameInfo.limit);
		}
	},
	_getGameTeamErrorCallBack:function(ret) {
	},
	// 大屏初始化end
	
	//检查加入人数
	_checkJoinPeople:function() {
		that.checkTimer = setInterval(function() {
			that._getJoinPeople();
		},2000);
	},
	_getJoinPeople:function() {
		var data = {meet_id:that.meet_id,sequence:that.sequence};
		that.request_data(that.check_url,data,that._JoinPeopleCallBack,that._JoinPeopleErrorCallBack);
	},
	_JoinPeopleCallBack:function(ret) {
		var leftObj = $("#section1").children(".left");
		var rightObj = $("#section1").children(".right");
		var lNum = parseInt(ret.lNum);
		var rNum = parseInt(ret.rNum);
		if(lNum <= that.game_params.limit) {
			if (lNum != -1) {
				leftObj.find(".man-num").html(lNum);
				if (lNum == that.game_params.limit)
					leftObj.find(".full").show();
				else
					leftObj.find(".full").hide();
			} else {
				leftObj.find(".full").hide();
			}
		}
		if(rNum <= that.game_params.limit) {
			if(rNum != -1) {
				rightObj.find(".man-num").html(rNum);
				if (rNum == that.game_params.limit)
					rightObj.find(".full").show();
				else
					rightObj.find(".full").hide();
			} else {
				rightObj.find(".full").hide();
			}
		}
	},
	_JoinPeopleErrorCallBack:function(ret) {
	},
	// 检查加入人数end

	//游戏开始
	_gameStart:function(flag) {
		$("#section2").show();
		that.getGameTime();
		that.readyGo(flag);
	},
	//获取游戏时间
	getGameTime:function() {
		that.getTimer = setInterval(function () {
			var data = {
				meet_id: that.meet_id,
				sequence: that.sequence,
			};
			that.request_data(that.game_time, data, that._getGameTimeCallBack, that.__getGameTimeErrorCallBack);
		}, 1000);
	},
	_getGameTimeCallBack:function(ret) {
		var time = ret.time;
		var obj = $("#countdown");
		if( ret.code == 1){
			obj.html(time);
			that._getPower();
		} else {
			that.showTransitionPage();
			clearInterval(that.getTimer);
		}
	},
	_getGameTimeErrorCallBack:function(){
	},
	// get power 获取力量
	_getPower:function() {
		var data = {meet_id:that.meet_id,sequence:that.sequence};
		that.request_data(that.get_power_url,data,that._getPowerCallBack,that._getPowerErrorCallBack);
	},
	_getPowerCallBack:function(ret) {
		var powerLeft = ret.left * 1;
		var powerRight = ret.right * 1;
		that.powerobj_l.html(powerLeft);
		that.powerobj_r.html(powerRight);
		var difference = Math.abs(powerLeft - powerRight) / 2;
		var position;
		if(powerLeft == powerRight) {
			position = 365;
		}else{
			position = difference > that.maxDifference ? that.maxDifference : difference;
			if(powerLeft < powerRight) {
				position = that.positionLeft - position;
			}else {
				position = that.positionLeft + position;
			}
		}
		var time = that.selectfrom(4,8)/10;
		var property = that.property;
		$("#arena-man").css({
			marginLeft: - position + "px",
		});
		$("#arena-man")[0].style.transition = "all" + time + "s ease";

	},
	_getPowerErrorCallBack:function(error) {
	},
	//显示 tips  ready/go
	readyGo:function(flag) {
		clearInterval(that.effectHorseTimer);
		if(flag == 1) {
			that.playMusic(that.aud,'readygo');
				clearInterval(that.checkTimer);
			var timer = setTimeout(function() {
				$("#ready").css("left","50%");
				clearTimeout(timer);
			}, 2500);
			var timerA = setTimeout(function() {
				$("#ready").css("left","-9999px");
				$("#go").css("left","50%");
				clearTimeout(timerA);
			}, 4000);
			var timerB = setTimeout(function() {
				$("#go").css("left","9999px");
				clearTimeout(timerA);
				that._openDoor();
				that.playMusic(that.audA,'start',that.bgMusic);
			}, 5000);
		}else {
			that._openDoor();
		}
		that._getParticipation();
	},
	//显示第二屏
	_openDoor:function() {
		var obj = $("#section1");
			obj.children(".left").css("left","-900px");
			obj.children(".right").css("right","-900px");
			obj.children(".tit").css("left","-900px");
			obj.children(".v").css("left","-900px");
			obj.children(".s").css("right","-900px");
			obj.children("#j_roundDiv").css("right","-900px");
	},

	// 获取参与人数
	_getParticipation:function() {
		var data = {
			meet_id:that.meet_id,
			sequence:that.sequence,
			round:that.game_params.round
		   };
		that.request_data(that.get_participation_url,data,that._getParticipationCallBack,that._getParticipationErrorCallBack);
	},
	_getParticipationCallBack:function(ret) {
		$("#sofa-red,#sofa-blue").find(".man-num").html(ret.players);
		var l_user_Arr = ret.l_u;
		var r_user_Arr = ret.r_u;
		var obj;
		
		//填充红沙发头像
		obj = $("#sofa-red").find(".swiper-wrapper");
		that.fillData(l_user_Arr,obj);

		//填充蓝沙发头像
		obj = $("#sofa-blue").find(".swiper-wrapper");
		that.fillData(r_user_Arr,obj);

		that._startSlider();
	},
	// slider item 回填
	fillData:function(data,obj) {
		var i = 0,j = 0,json,html = [];
		for(var key in data) {
			json = data[key];
			j = i++;
			if( !(j % 10) ){
				html.push('<li class="clearfix slider-item swiper-slide">');
			}
			html.push('<div class="fl">');
			html.push('<div>');
			html.push('<img src="'+json.headimgurl+'" class="responseImg" alt="" title="" />');
			html.push('</div>');
			html.push('<p class="nickname textset">'+json.nickname+'</p>');
			html.push('</div>');
			if(!(++j % 10) && j >= 10) {
				html.push("</li>");
			}
		}
		if(!/(^[0-9]\d*$)/.test(i/10)) {
			html.push("</li>");
		}
		obj.html(html.join(''));
	},
	_getParticipationErrorCallBack:function(error) {
	},

	showTransitionPage:function() {
		$("#containter").css("height","0px");
		that.statistic();
	},
	// 显示结果页
	statistic:function() {
		var data = {
			sequence:that.sequence,
			round:that.round,
			meet_id: that.meet_id
		};
		that.request_data(that.statistic_url,data,that.statisticCallback,that.statisticErrorCallback);	
	},
	statisticCallback:function(ret) {
		if(ret.res ==1) {
			//将游戏数据存sessionStorage
			that.setItems("resultInfo",JSON.stringify(ret));
			var timer = setTimeout(function() {
			clearTimeout(timer);
			window.location.href = '/tug/screen/result' + window.location.search;
		}, 5000);
		} else {
			alert(ret.msg);
		}
	},
	statisticErrorCallback:function(ret){

	},
	//跑马灯效果
	_effectHorse:function() {
		var left = $("#rule-tips").innerWidth();
		$("#rule-tips").animate({"left": "-"+left+"px"},20000,function() {
				$("#rule-tips").css("left","100%");
			});
		that.effectHorseTimer = setInterval(function() {
			$("#rule-tips").animate({"left":"-100%"},20000,function() {
				$("#rule-tips").css("left","100%");
			});
		},20000);
	},

	// 第二屏头像轮播
	_startSlider:function() {
		var swiper = new Swiper('#sofa-L-container', {
		    pagination: '.swiper-pagination-L',
		    paginationClickable: true,
		    spaceBetween: 30,
		    autoplay:3000,
		    loop:true
		});
		var swiper = new Swiper('#sofa-R-container', {
		    pagination: '.swiper-pagination-R',
		    paginationClickable: true,
		    spaceBetween: 30,
		    autoplay:3000,
		    loop:true
		});
	},
	// 播放音乐
	playMusic:function(obj,flag,index) {
		if(flag == 'readygo') {
			obj.src = '/music/readygo.mp3';
			that.stopMusic(that.audA);
		}
		if(flag == 'start') {
			obj.src = '/music/'+index+'.mp3';
		}
		obj.play();
	},
	// 暂停
	stopMusic:function(obj) {
		obj.pause();
	},
	//ajax 请求
	request_data:function(url,data,callback,errorCallback) {
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			data:data,
			success:function(ret) {
				callback && callback(ret);
			},
			error:function() {
				errorCallback && errorCallback();
			}
		})
	},

	selectfrom:function(minValue,maxValue){
		var choice = maxValue-minValue+1;
		return Math.floor(Math.random()*choice+minValue);
	},
	isSuportCss:function(property){
		var body = $("body")[0];
		var length = property.length;
		for(var i=0; i<length;i++){
			if(property[i] in body.style){
				return true;
			}
		}
		return false;
	},
	
	setItems:function(key,value) {
		sessionStorage.setItem(key,value);
	},
	getItems:function(key) {
		return sessionStorage.getItem(key);
	},
	getQueryString:function(name) { 
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
        var r = window.location.search.substr(1).match(reg); 
        if (r != null) return unescape(r[2]); return null; 
    }
};
